#include "register.h"


int do_register(user_session_t *user_session)
{
    tlv_msg_t msg;
    bzero(&msg,sizeof(msg));
    tlv_recv(user_session->netfd,&msg);
    char username[4096]={0};
    memcpy(username, msg.value, msg.hdr.length);
    char password[4096]={0};
    bzero(&msg,sizeof(msg));
    tlv_recv(user_session->netfd,&msg);
    memcpy(password, msg.value, msg.hdr.length);
    char sql[4096]={0};
    sprintf(sql,"select username,is_deleted from users where username ='%s';",username);
    int qres=mysql_query(user_session->conn,sql);
    if(qres!=0)
    {
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(user_session->conn));
        exit(-1);
    }
    printf("%s\n",sql);
    MYSQL_RES *table=mysql_store_result(user_session->conn);
    MYSQL_ROW row=mysql_fetch_row(table);
    if(row==NULL)
    {
        printf("user does not exist.\n");
        
        //获取盐值，将用户信息插入数据库
        char salt[21]={0};
        getsalt(salt);
        printf("%s\n",salt);
        char encry_password[1024]={0};
        strcpy(encry_password,crypt(password,salt));
        printf("%s\n",encry_password);

        //insert into username;
        char sql[4096]={0};
        sprintf(sql,"insert into users(username,salt,encrypted_password,is_deleted) values ('%s','%s','%s','%d');",
                                       username,salt,encry_password,0);
        puts(sql);
        int qres=mysql_query(user_session->conn,sql);
        if(qres!=0)
        {
           fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(user_session->conn));
           exit(-1);
        }
           tlv_msg_t msg;
           bzero(&msg,sizeof(tlv_msg_t));
           char value[]="user register success.";
           msg.hdr.length=strlen(value);
           tlv_create(&msg,LOGIN_IN,msg.hdr.length,value);
           tlv_send(user_session->netfd,&msg);
           return 0;
    }
    else
    {
        if(atoi(row[1])==1)
        {
            printf("user does not exist.\n");
            printf("is delete\n");
            printf("%s\n",row[1]);

            //获取盐值，将用户信息更新
            char salt[21]={0};
            getsalt(salt);
            printf("%s\n",salt);
            char encry_password[1024]={0};
            strcpy(encry_password,crypt(password,salt));
            printf("%s\n",encry_password);

            //update
            char sql[4096]={0};
            sprintf(sql,"update users set salt='%s',encrypted_password='%s',is_deleted=0 where username ='%s';",
                    salt,encry_password,username);
            puts(sql);
            int qres=mysql_query(user_session->conn,sql);
            if(qres!=0)
            {
              fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(user_session->conn));
              exit(-1);
            }
            tlv_msg_t msg;
            bzero(&msg,sizeof(tlv_msg_t));
            char value[]="user register success.";
            msg.hdr.length=strlen(value);
            tlv_create(&msg,LOGIN_IN,msg.hdr.length,value);
            tlv_send(user_session->netfd,&msg);
            printf("re_register success.\n");
            return 0;
        }
        else
        {
            tlv_msg_t msg;
            bzero(&msg,sizeof(tlv_msg_t));
            char value[]="user already exists.";
            msg.hdr.length=strlen(value);
            tlv_create(&msg,LOGIN_FALSE,msg.hdr.length,value);
            tlv_send(user_session->netfd,&msg);
            printf("user already exists.\n");
            return -1;
        }
    }
    return 0;
}